﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using System.Text.RegularExpressions;
using DevExpress.Utils;
using System.Xml;
using System.IO;
using ConfExpert.Utility;

namespace ConfExpert.Forms.Depo
{
    public partial class StokDuzenleme2 : XForm
    {
        string[] patterns = {@"[F]\w{5,7}/\w{6}", @"^(FA)\w{6}/\w{6}", @"\w{3}-\w{4}/\w{4}", @"\w{4}-\w{4}/\w{4}", @"\w{4,5}/\w{4}", 
                                    @"\w{4}-\w{4}/\w{4}|\w{5}-\w{5}/\w{4}|\w{6}-\w{6}/\w{4}", @"\w{5}/\w{5}", @"\w{4}-\w{4}/\w{6}", @"\w{5}-\w{4}/\w{2}", 
                                    @"\w{3}-(\w{2}-)?\w{4}/\w{3}", @"[U]\w{4}-\w{4}/\w{4}|\w{5}-\w{6}/\w{4}", @"[U]\w{4}-\w{4}/\w{4}|\w{5}-\w{6}/\w{4}", @"\w{4}-\w{4}/\w{4}", 
                                    @"\w{4}-\w{4}/\w{3}", @"[F]\w{5,7}/\w{6}", @"\w{5}-\w{4}/\w{2}|\w{5}-\w{4}/\w{3}", @"\w{1,}-\w{4}/\w{6}"};
        public StokDuzenleme2()
        {
            InitializeComponent();
        }

        private void StokDuzenleme2_Load(object sender, EventArgs e)
        {
            this.Text = "Stok Düzenleme";

            Dictionary<int, string> arts = new Dictionary<int, string>();
            foreach (DataRow r in AppSession.Lookups.V_Kumas.Rows)
                arts.Add(r["ProductId"].ToInt(), r["Name"].ToStr());

            DataTable dtNew = new DataTable();
            dtNew.Clear();
            dtNew.Columns.Add("ProductId", typeof(int));
            dtNew.Columns.Add("MaskNr", typeof(int));

            foreach (KeyValuePair<int,string> r in arts)
            {
                for (int i = 0; i < patterns.Length; ++i)
                {
                    Regex regex = new Regex(patterns[i]);
                    Match Eslesen = regex.Match(r.Value);

                    if (regex.IsMatch(r.Value) && Eslesen.ToString().Equals(r.Value))
                    {
                        DataRow NewRow = dtNew.NewRow();
                        NewRow["ProductId"] = r.Key;
                        NewRow["MaskNr"] = i + 1;
                        dtNew.Rows.Add(NewRow);
                    }
                }
            }

            gridView1.Init("GetStokDuzenleme", dtNew.Serialize());

            gridView1.SetToolTip("1:ALBINI", "2:ALBIATE", "3:ANDREAZZA & CASTELLI", "4:ARCOTEX", "5:BRENNET", "6:CANCLINI", "7:GRANDI RUBINELLI", "8:LEGGIUNO", "9:MILETA", "10:MONTI",
                                        "11:PROFILO TESSILE", "12:PROFILO CHINA", "13:TEKSTINA", "14:TESTA", "15:THOMAS MASON", "16:WEBA", "17:SOMELOS");
            
            gridView1.MakeColumnInvisible("ProductId");
            gridView1.MakeReadOnly("Name", "KVendorId", "KCustomerId");
            gridView1.AddSummary("ProductId");

            gridView1.RowCellStyle += gridView1_RowCellStyle;

            gridView1.BestFitColumns();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                switch (e.Column.FieldName)
                {
                    case "Name":
                    case "KVendorId":
                    case "KCustomerId":
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                        return;
                }
            }
        }

        private void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            string xml = gridView1.SerializeChanges();
            ExecuteNonQuery("UpdProduct", xml);

            DataSet d = new DataSet();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xml));
            d.ReadXml(xmlReader);
            DataTable DegisenSatirlar = d.Tables[0];

            foreach (DataRow dr in DegisenSatirlar.Rows)
            {
                int RowHandle = DevExpress.XtraGrid.GridControl.InvalidRowHandle;

                for (int i = 0; i < gridView1.DataRowCount; i++)
                    if (gridView1.GetDataRow(i)["ProductId"].ToInt() == dr["ProductId"].ToInt())
                    {
                        RowHandle = i;
                        break;
                    }

                for (int i = 0; i < patterns.Length; ++i)
                {
                    Regex regex = new Regex(patterns[i]);
                    Match Eslesen = regex.Match(dr["Name"].ToStr());
                    
                    if (regex.IsMatch(dr["Name"].ToStr()) && Eslesen.ToString().Equals(dr["Name"].ToStr()))
                    {
                        gridView1.SetRowCellValue(RowHandle, (i + 1).ToStr(), i + 1);
                    }
                    else
                        gridView1.SetRowCellValue(RowHandle, (i + 1).ToStr(), null);
                }
            }

            Notify("Güncelleme Başarılı.");
        }

        private int FindRowHandleByDataRow(DevExpress.XtraGrid.Views.Grid.GridView view, DataRow row)
        {

            if (row != null)

                for (int i = 0; i < view.DataRowCount; i++)

                    if (view.GetDataRow(i) == row)

                        return i;

            return DevExpress.XtraGrid.GridControl.InvalidRowHandle;

        }
    }
}
